<body>
    <h2>商业合同列表</h2>
    <div style="margin:10px 0;"></div>
    <table id="list"></table>
    <div id="tb" style="padding:5px;height:auto">
        <div style="margin-bottom:5px;vertical-align:middle">
            <a href="#" onclick="javascript:contractListJs.newContract()" class="easyui-linkbutton" iconCls="icon-add" plain="true">增加</a>
            <a href="#" onclick="javascript:contractListJs.edit()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
            <a href="#" class="easyui-linkbutton"  plain="true">过滤条件：</a>
         	<a href="#" onclick="javascript:contractListJs.openDateFilter()" class="easyui-linkbutton"  plain="true">时间</a>
    		

            
            <a href="#" id="employeeButton">添加人</a>
                <div id="mm3" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:middle;width:200px;height:auto">
    				<div class="ftitle">按照添加人搜索</div>
    			    <div id="employee_list">
            		</div> 
    			</div>
			<a href="#" class="easyui-menubutton" data-options="menu:'#mm2'">状态</a>
                <div id="mm2" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:middle;width:200px;height:60px">
    				<select class="easyui-combobox" name="strategy" id="strategy" onchange="javascript:contractListJs.searchByStrategy()">
    					<option></option>
                		<option value="processing">进行中</option>
                		<option value="success">已完成</option>
                		<option value="failed">已取消</option>
            		</select>
    			</div>
        </div>
        <div id="contract_date" style="padding:3px">
        	从:<input id="contract_from" type="text"></input>
        	到:<input id="contract_to" type="text"></input>
        	<span class="toolbar_pic"><a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:contractListJs.searchByTime()" >搜索</a></span>
        </div>
        <div id="searchFilter">
        </div>
    </div>
    <div id="ContractDetail" title="合同详细信息" class="easyui-dialog" closed="true" style="width:850px;height:550px">
    </div>
    <script type="text/javascript">
    
(function(contractListJs, $, undefined ) {    
	var countPerPage = 5;
	var start = 0;
	
	$('#list').datagrid({
		title:'商业合同',
		iconCls:'icon-contract',
		height:300,
		fitColumns:true,
		singleSelect:true,
		url:null,
		rownumbers:true,
	    columns:[[
					{title:'客户',field:'customerName',width:80},
	             	{title:'商业机会',field:'opportunityName',width:80},
	             	{title:'状态',field:'strategy',width:80,formatter:setStrategyFormat},
	             	{title:'手术时间',field:'time',width:80},
	             	{title:'添加人',field:'createUser',width:80},
	             	{title:'添加时间',field:'createTime',width:80},
	             ]],
		pagination:true,
        pageSize:countPerPage,
        pageList:[5,10,15,20,25],
        toolbar:'#tb',
	    onLoadSuccess: function(){   
	        $('#list').datagrid('loaded');
	    },

	    onDblClickCell: openContract
	});
	$('#list').datagrid('getPager').pagination({   
	    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录',   
	    onSelectPage : function(pPageIndex, pPageSize) {   
	    	
	        var gridOpts = $('#list').datagrid('options');   
	        gridOpts.pageNumber = pPageIndex;   
	        gridOpts.pageSize = pPageSize;     

	        start = (pPageIndex -1)*pPageSize;
	        workbenchJs.loadData("dispatch/customer/contract/all?start="+start+'&size='+countPerPage,contractListJs.loadContractData,"查询手术合同");
	    },
	    onChangePageSize : function(pageSize){
	    	countPerPage = pageSize;
	    }
	}); 
	contractListJs.init = function(){
		$('#contract_from').datebox();
		$('#contract_to').datebox();
		refresh();
	};
	contractListJs.newContract = function(){
		workbenchJs.open1('component/customer/addContract.html',this);
	};
	contractListJs.edit = function(){
		var row = $('#list').datagrid('getSelected');
        if (row){
    		workbenchJs.open1('component/customer/editContract.jsp?id='+row.id,this);
        }else
        	{
        		$.messager.alert("Error","请选择一条记录");
        	}
		
	};
	function setStrategyFormat(value){
		if(value == 'processing')
			return "进行中";
		if(value == 'success')
			return '完成';
		if(value == 'failed')
			return '已取消';
	}

	contractListJs.loadContractData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	$('#list').datagrid('loadData',payload);
        }else if(result.result == "NOT_AUTHORIZED")
        {
        	$.messager.alert("Error","操作: 查询合同信息  错误信息: 您没有权限 ");
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
        $('#list').datagrid('loaded');
	};
	
	function openContract(){
        var row = $('#list').datagrid('getSelected');
        if (row){
    		$('#ContractDetail').dialog("open").dialog('setTitle','合同详细信息');
    		$('#ContractDetail').dialog('refresh','component/customer/viewContract.jsp?id='+row.id);
        }else
        	{
        		$.messager.alert("Error","请选择一条记录");
        	}
	}

    function refresh(){
		$('#contract_date').hide();
		workbenchJs.loadData("dispatch/customer/contract/all?start="+start+"&size="+countPerPage,contractListJs.loadContractData,"查询手术合同");
		workbenchJs.loadData("dispatch/manage/employee/simpleAll",contractListJs.loadEmployeeResult,"查询用户列表");
    }
    
    contractListJs.loadEmployeeResult = function(result){
    	if(result.result == "SUCCESS")
    	{
    		var rows = jQuery.parseJSON(result.payload.value);
    		var html = "";
    		html+='<p><a href="#" onclick="javascript:contractListJs.searchByEmployee(\'*\')"> 全部</a></p>';
    		$(rows).each(function(index){
    			html+='<p><a href="#" onclick="javascript:contractListJs.searchByEmployee(\''+rows[index].name+'\')"> '+rows[index].name+' </a></p>';
    		});
    		$('#employee_list').html(html);
    		$('#employeeButton').menubutton({
    		    menu: '#mm3'
    		    });
    	}
    };
	contractListJs.clearSearch = function()
	{
		countPerPage = 5;
		start = 0;
	    consult_employee_search = '';
	    consult_strategy_search = '';
		$('#contract_from').datebox('setValue','');
		$('#contract_to').datebox('setValue','');
		refreshSearchFilter();
		refresh();
	}
	
	contractListJs.openDateFilter = function(){
		$('#contract_date').show();
	}
	
    var consult_employee_search = '';
    var consult_strategy_search = '';
	contractListJs.searchByEmployee = function(employee){
		consult_employee_search = employee;
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}

	contractListJs.searchByTime = function(){
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}
	contractListJs.searchByStrategy = function(){
		consult_strategy_search = $('#strategy').val();
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}
	function getSearchFilter()
	{
		var html = "查询条件:";
		if(consult_employee_search != "" && typeof(consult_employee_search) != 'undefined')
			html += " + "+consult_employee_search;
		
		if(consult_strategy_search != "" && typeof(consult_strategy_search) != 'undefined')
		{
			html += " + "+setStrategyFormat(consult_strategy_search);
		}
		
		var contract_from = $('#contract_from').datebox('getValue');
		if(contract_from != "" && typeof(contract_from) != 'undefined')
			html += " + 时间从 "+contract_from;
		
		var contract_to = $('#contract_to').datebox('getValue');
		if(contract_to != "" && typeof(contract_to) != 'undefined')
			html += " + 时间到 "+contract_to;
		
		return html;
	}
	function refreshSearchFilter()
	{

		var html = getSearchFilter();
		html += ' <a href="javascript:void(0)" onclick="javascript:contractListJs.clearSearch()">清除</a>';
		$('#searchFilter').html(html);
		
	}
	function postSearch()
	{
		var str = "";
		var contract_from = $('#contract_from').datebox('getValue');
		if(typeof(contract_from) == 'undefined')
			contract_from = '';
		
		var contract_to = $('#contract_to').datebox('getValue');
		if(typeof(contract_to) == 'undefined')
			contract_to = '';
		
		str = appendURL(str,"employee="+consult_employee_search);
		str = appendURL(str,"strategy="+consult_strategy_search);
		str = appendURL(str,"from="+contract_from);
		str = appendURL(str,"to="+contract_to);
		str = appendURL(str,'start='+start+'&size='+countPerPage);
		$.ajax({
            type: "post",
            url: "dispatch/customer/contract/all",
            dataType:"json",
            data: str,
            cache: false,
            success: function (result) {
            	
            	contractListJs.loadContractData(result);
            }
		});
	}
	function appendURL(url,str1)
	{
		if(typeof(str1) == 'undefined')return url;
		if(url == "" || typeof(url) == 'undefined')url = str1;
		else url+="&"+str1;

		return url;
	}
}( window.contractListJs = window.contractListJs || {}, jQuery ));

$(function(){
	contractListJs.init();
});    	

    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
        .toolbar_pic {
 			width: 70px;
  			height: 30px;
  			overflow: hidden;
  			display: inline-block;
  			vertical-align: top;
  			cursor: pointer;
  			opacity: 0.6;
  			filter: alpha(opacity=60);
		}
		.toolbar_text {
 			width: 70px;
 			height:100%;
  			overflow: hidden;
  			display: inline-block;
  			vertical-align: middle;
		}
    </style>